//
//  CrmCustomerCell.m
//  New_Runway
//
//  Created by mack on 2018/2/24.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CrmCustomerCell.h"

@interface CrmCustomerCell ()

@end

@implementation CrmCustomerCell
{
    UIImageView *followStateImgV;
    UILabel *nameLabel;
    UILabel *phoneLabel;
    UILabel *cityLabel;
    UILabel *levelLabel;
    UILabel *timeLabel;
    UIButton *distributionBtn;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupUI];
        [self layoutUI];
    }
    return self;
}

- (void)setupUI {
    
    followStateImgV = DPImageView().setBackgroundColor([UIColor whiteColor]);
    
    nameLabel = DPLabel()
    .setFont(kPingFangRegular14())
    .setTextColor(getColorFromString(@"#323232"))
    .setBackgroundColor([UIColor whiteColor]);
    
    phoneLabel = DPLabel()
    .setFont(kPingFangRegular14())
    .setTextColor(getColorFromString(@"#323232"))
    .setBackgroundColor([UIColor whiteColor]);
    
    cityLabel = DPLabel()
    .setFont(kPingFangRegular14())
    .setTextColor(getColorFromString(@"#323232"))
    .setBackgroundColor([UIColor whiteColor]);
    
    levelLabel = DPLabel()
    .setFont(kPingFangRegular14())
    .setTextColor(getColorFromString(@"#323232"))
    .setBackgroundColor([UIColor whiteColor]);
    
    timeLabel = DPLabel()
    .setFont(kPingFangRegular14())
    .setTextColor(getColorFromString(@"#989898"))
    .setBackgroundColor([UIColor whiteColor]);
    
    distributionBtn = DPButton()
    .setNormalImage(getImageFromName(@"crm_distribution_n"))
    .setHighlightImage(getImageFromName(@"crm_distribution_h"))
    .addClickAction(self, @selector(distributionBtnDidClick:));
    
    self.contentView
    .addSubview(followStateImgV)
    .addSubview(nameLabel)
    .addSubview(phoneLabel)
    .addSubview(cityLabel)
    .addSubview(levelLabel)
    .addSubview(timeLabel)
    .addSubview(distributionBtn);
}

- (void)layoutUI {
    
    [followStateImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.contentView);
        make.width.equalTo(@60);
        make.height.equalTo(@20);
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.left.equalTo(self.contentView.mas_left).offset(16);
        make.height.equalTo(@13);
    }];
    
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(26);
        make.centerY.equalTo(nameLabel.mas_centerY);
    }];
    
    [cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_right).offset(26);
        make.centerY.equalTo(phoneLabel.mas_centerY);
    }];
    
    [distributionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(followStateImgV.mas_bottom).offset(13);
        make.right.equalTo(self.contentView.mas_right).offset(-16);
        make.width.equalTo(@70);
        make.height.equalTo(@30);
    }];
    
    [levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(15);
        make.left.equalTo(nameLabel.mas_left);
        make.height.equalTo(@13);
    }];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(levelLabel.mas_bottom).offset(15);
        make.left.equalTo(levelLabel.mas_left);
        make.height.equalTo(@13);
    }];
}

- (void)distributionBtnDidClick:(UIButton *)sender {
    
    if ([sender.titleLabel.text isEqualToString:@"分配"]) {
        [self routerEventWithName:@"distribution" userInfo:@{@"index" : self.index}];
        
    }else if ([sender.titleLabel.text isEqualToString:@"详情"]){
        [self routerEventWithName:@"detail" userInfo:@{@"index" : self.index}];
    }
}

- (void)setShowdDistributionBtn:(BOOL)showdDistributionBtn {
    
    _showdDistributionBtn = showdDistributionBtn;
    
    distributionBtn.hidden = !showdDistributionBtn;
}

-(void)setCrmInfo:(CrmCustomerResult *)CrmInfo{
    _CrmInfo = CrmInfo;
    if (CrmInfo.customerInfo.followState == 0) {
        [distributionBtn setTitle:@"分配" forState:UIControlStateNormal];
    }else{
        [distributionBtn setTitle:@"详情" forState:UIControlStateNormal];
    }
    
    if (CrmInfo.customerInfo.followState == 0) {
        followStateImgV.image = [UIImage imageNamed:@""];
    }else if (CrmInfo.customerInfo.followState == 1){
        followStateImgV.image = [UIImage imageNamed:@"crm_willFollow"];
    }else if (CrmInfo.customerInfo.followState == 10){
        followStateImgV.image = [UIImage imageNamed:@"crm_following"];
    }else if (CrmInfo.customerInfo.followState == 20){
        followStateImgV.image = [UIImage imageNamed:@"crm_arrange"];
    }else if (CrmInfo.customerInfo.followState == 30){
        followStateImgV.image = [UIImage imageNamed:@"crm_fail"];
    }else if (CrmInfo.customerInfo.followState == 40){
        followStateImgV.image = [UIImage imageNamed:@"crm_nouse"];
    }else if (CrmInfo.customerInfo.followState == 50){
        followStateImgV.image = [UIImage imageNamed:@"crm_successful"];
    }
//    NSString *nameStr = CrmInfo.customerInfo.customer.customerName.length == 0? @"": CrmInfo.customerInfo.customer.customerName;
    NSString *nameStr = nil;
    if (isEmptyOrNilString(CrmInfo.customerInfo.customer.customerName)) {
        if (isEmptyOrNilString(CrmInfo.customerInfo.customer.customerNickname)) {
            nameStr = @"";
        } else {
            nameStr = CrmInfo.customerInfo.customer.customerNickname;
        }
    } else {
        nameStr = CrmInfo.customerInfo.customer.customerName;
    }
    if (nameStr.length > 4) {
        nameLabel.text = [NSString stringWithFormat:@"%@...", [nameStr substringToIndex:4]];
    } else {
        nameLabel.text = nameStr;
    }
    phoneLabel.text = CrmInfo.customerInfo.customer.customerPhone.length == 0? @"": CrmInfo.customerInfo.customer.customerPhone;
    cityLabel.text = CrmInfo.customerInfo.customer.cityName.length == 0? @"": CrmInfo.customerInfo.customer.cityName;
    levelLabel.text = [NSString stringWithFormat:@"客户等级: %@", CrmInfo.customerInfo.levelTxt.length == 0? @"": CrmInfo.customerInfo.levelTxt];
    timeLabel.text = CrmInfo.customerInfo.customer.createTime.length == 0? @"": CrmInfo.customerInfo.customer.createTime;
}


@end
